package tv.huan.media.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tv.huan.common.annotation.DataScope;
import tv.huan.common.core.text.Convert;
import tv.huan.common.utils.DateUtils;
import tv.huan.media.domain.Album;
import tv.huan.media.domain.Media;
import tv.huan.media.domain.MediaAudit;
import tv.huan.media.domain.TeamUser;
import tv.huan.media.mapper.*;
import tv.huan.media.service.IMediaService;
import tv.huan.system.domain.SysUser;
import tv.huan.system.mapper.SysDeptMapper;

/**
 * 媒资Service业务层处理
 * 
 * @author admin
 * @date 2020-04-02
 */
@Service
public class MediaServiceImpl implements IMediaService 
{
    @Autowired
    private MediaMapper mediaMapper;

    @Autowired
    private TeamUserMapper teamUserMapper;

    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private TencentInfoMapper tencentInfoMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private MediaAuditMapper mediaAuditMapper;

    @Value("${huan.sourceAddress}")
    private String sourceAddress;

    /**
     * 查询媒资
     * 
     * @param id 媒资ID
     * @return 媒资
     */
    @Override
    public Media selectMediaById(Integer id)
    {
        return mediaMapper.selectMediaById(id);
    }

    /**
     * 查询媒资列表
     * 
     * @param media 媒资
     * @return 媒资
     */
    @Override
    @DataScope(deptAlias = "ma")
    public List<Media> selectMediaList(Media media)
    {
        return mediaMapper.selectMediaList(media);
    }

    //审核小组数据权限，设置teamId
    @Override
    public Media setMediaTeamId(Media media, SysUser user){
        Integer teamId;
        TeamUser tu = new TeamUser();
        tu.setUserId(user.getUserId());
        List<TeamUser> list = teamUserMapper.selectTeamUserList(tu);
        if (null != list && list.size() > 0 ){
            teamId = list.get(0).getTeamId();
            media.setTeamId(teamId);
        }
        return media;
    }

    /**
     * 新增媒资
     * 
     * @param media 媒资
     * @return 结果
     */
    @Override
    public int insertMedia(Media media)
    {
        media.setCreateTime(DateUtils.getNowDate());
        return mediaMapper.insertMedia(media);
    }

    /**
     * 修改媒资
     * 
     * @param media 媒资
     * @return 结果
     */
    @Override
    public int updateMedia(Media media)
    {
        media.setUpdateTime(DateUtils.getNowDate());
        return mediaMapper.updateMedia(media);
    }

    /**
     * 删除媒资对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMediaByIds(String ids)
    {
        return mediaMapper.deleteMediaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除媒资信息
     * 
     * @param id 媒资ID
     * @return 结果
     */
    public int deleteMediaById(Integer id)
    {
        return mediaMapper.deleteMediaById(id);
    }

    //接受推送数据并初始化
    @Transactional
    public void initData(JSONObject auditJson){
        Date now = new Date();
        JSONObject initMedia = auditJson.getJSONObject("media");
        String vid = initMedia.getString("vid");
        JSONObject initAlbum = auditJson.getJSONObject("album");
        String cid = initAlbum.getString("cid");
        String url = !"".equals(initMedia.getString("url")) ? initMedia.getString("url") : sourceAddress + "media/" + vid + "/index.m3u8";
        String picPath = !"".equals(initMedia.getString("pic_path")) ? initMedia.getString("pic_path") : sourceAddress + "picture/" + cid + "_album_hz/0.jpg";
        String newPicVt = !"".equals(initAlbum.getString("new_pic_vt")) ? initAlbum.getString("new_pic_vt") : sourceAddress + "picture/" + cid + "_album_vt/0.jpg";
        String newPicHz = !"".equals(initAlbum.getString("new_pic_hz")) ? initAlbum.getString("new_pic_hz") : sourceAddress + "picture/" + cid + "_album_hz/0.jpg";
        Media exitMedia = mediaMapper.selectMediaByVid(vid);
        if (null == exitMedia){
            Media media = new Media();
            media.setVid(vid);
            media.setTitle(initMedia.getString("title"));
            media.setCid(initMedia.getString("cid"));
            media.setAlbumTitle(initMedia.getString("album_title"));
            media.setDescription(initMedia.getString("description"));
            media.setLangue(initMedia.getString("langue"));
            media.setLeadingActor(initMedia.getString("leading_actor"));
            media.setDirector(initMedia.getString("director"));
            media.setYear(initMedia.getString("year"));
            media.setAreaName(initMedia.getString("area_name"));
            media.setScore(initMedia.getString("score"));
            media.setType(initMedia.getInteger("type"));
            media.setDuration(initMedia.getInteger("duration"));
            media.setEpisode(initMedia.getString("episode"));
            media.setPublishDate(initMedia.getString("publish_date"));
            media.setUrl(url);
            media.setPicPath(picPath);
            media.setCreateTime(now);
//		    mediaJson.put("status",0);
            mediaMapper.insertMedia(media);
            Integer mediaId = mediaMapper.selectLastMediaId();
            MediaAudit mediaAudit = new MediaAudit();
            String[] deptNames = tencentInfoMapper.selectTencentInfoById(1).getAuditDeptName().split(",");
            Long deptId = sysDeptMapper.selectDeptByName(deptNames[0]).getDeptId();
            mediaAudit.setMediaId(mediaId);
            mediaAudit.setDeptId(deptId);
            mediaAuditMapper.insertMediaAudit(mediaAudit);
        }else {
            exitMedia.setVid(vid);
            exitMedia.setTitle(initMedia.getString("title"));
            exitMedia.setCid(initMedia.getString("cid"));
            exitMedia.setAlbumTitle(initMedia.getString("album_title"));
            exitMedia.setDescription(initMedia.getString("description"));
            exitMedia.setLangue(initMedia.getString("langue"));
            exitMedia.setLeadingActor(initMedia.getString("leading_actor"));
            exitMedia.setDirector(initMedia.getString("director"));
            exitMedia.setYear(initMedia.getString("year"));
            exitMedia.setAreaName(initMedia.getString("area_name"));
            exitMedia.setScore(initMedia.getString("score"));
            exitMedia.setType(initMedia.getInteger("type"));
            exitMedia.setDuration(initMedia.getInteger("duration"));
            exitMedia.setEpisode(initMedia.getString("episode"));
            exitMedia.setPublishDate(initMedia.getString("publish_date"));
            exitMedia.setUrl(url);
            exitMedia.setPicPath(picPath);
            exitMedia.setCreateTime(now);
            exitMedia.setStatus(0);
            mediaMapper.updateMedia(exitMedia);
            mediaAuditMapper.initMediaAudit(exitMedia.getId());
        }
        Album exitAlbum = albumMapper.selectAlbumByCid(cid);
        if (null == exitAlbum){
            Album album = new Album();
            album.setCid(cid);
            album.setTitle(initAlbum.getString("title"));
            album.setSecondTitle(initAlbum.getString("second_title"));
            album.setType(initAlbum.getInteger("type"));
            album.setTag(initAlbum.getString("tag"));
            album.setCopyright(initAlbum.getString("copyright"));
            album.setLangue(initAlbum.getString("langue"));
            album.setDirector(initAlbum.getString("director"));
            album.setDescription(initAlbum.getString("description"));
            album.setEpisodeAll(initAlbum.getString("episode_all"));
            album.setEpisodeUpdated(initAlbum.getString("episode_updated"));
            album.setScore(initAlbum.getString("score"));
            album.setLeadingActor(initAlbum.getString("leading_actor"));
            album.setAreaName(initAlbum.getString("area_name"));
            album.setYear(initAlbum.getString("year"));
            album.setNewPicVt(newPicVt);
            album.setNewPicHz(newPicHz);
            album.setPublishDate(initAlbum.getString("publish_date"));
//            albumJson.put("team_id",0);
//            albumJson.put("status",0);
            album.setCreateTime(now);
            albumMapper.insertAlbum(album);
        }
    }

    public static void main(String[] args) {
        JSONObject auditJson = new JSONObject();
        JSONObject mediaJson = new JSONObject();
        mediaJson.put("vid","sdxlygb01");
        mediaJson.put("title","神雕侠侣01");
        mediaJson.put("cid","sdxlygb");
        mediaJson.put("album_title","神雕侠侣");
        mediaJson.put("description","神雕侠侣是一部精彩的电视剧...其中福娃恩负卡瓦尼个让杰哥更热一一二十日你是饿不死人吧vUI俄日染黑访问GG而分为非仍突然个人个人他个人个人个高投入高");
        mediaJson.put("langue","国语");
        mediaJson.put("leading_actor","杨过");
        mediaJson.put("director","窦明杨");
        mediaJson.put("year","2011");
        mediaJson.put("area_name","大陆");
        mediaJson.put("score","5.2");
        mediaJson.put("type",2);
        mediaJson.put("duration",45);
        mediaJson.put("episode","1");
        mediaJson.put("publish_date","2015-03-05");
        mediaJson.put("url","");
        mediaJson.put("pic_path","");
//		mediaJson.put("status",0);
//		mediaJson.put("create_time","");
        JSONObject albumJson = new JSONObject();
        albumJson.put("cid","sdxlygb");
        albumJson.put("title","神雕侠侣");
        albumJson.put("second_title","");
        albumJson.put("type",2);
        albumJson.put("tag","动作");
        albumJson.put("copyright","腾讯");
        albumJson.put("langue","国语");
        albumJson.put("director","窦明杨");
        albumJson.put("description","神雕侠侣是一部精彩的电视剧...其中福娃恩负卡瓦尼个让杰哥更热一一二十日你是饿不死人吧vUI俄日染黑访问GG而");
        albumJson.put("episode_all","");
        albumJson.put("episode_updated","");
        albumJson.put("score","5.5");
        albumJson.put("leading_actor","杨过");
        albumJson.put("area_name","大陆");
        albumJson.put("year","2011");
        albumJson.put("new_pic_vt","");
        albumJson.put("new_pic_hz","");
        albumJson.put("publish_date","2012-03-05");
//		albumJson.put("team_id",0);
//		albumJson.put("status",0);
        auditJson.put("media",mediaJson);
        auditJson.put("album",albumJson);
        System.out.println(auditJson.toString());
    }
}
